﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace RFPL
{
    public partial class GameInfo : Form
    {
        private int gameId = 0;

        private int fk_club1 = 0;
        private int fk_club2 = 0;

        public GameInfo(int gmId)
        {
            InitializeComponent();
            gameId = gmId;
        }

        private void GameInfo_Load(object sender, EventArgs e)
        {
            setGameInfo();
            UpdateGoals();

        }

        private void setGameInfo() {

            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = Global.connectionString;
            conn.Open();
            SqlCommand myCommand = conn.CreateCommand();
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.CommandText = "[getGameInfo]";
            myCommand.Parameters.Add("@id", SqlDbType.Int).Value = gameId;
            SqlDataReader dataReader = myCommand.ExecuteReader();
            if (dataReader.Read())
            {
                fk_club1 = Convert.ToInt32(dataReader[0]);
                lPlayer1.Text = Convert.ToString(dataReader["club1"]);
                fk_club2 = Convert.ToInt32(dataReader[2]);
                lPlayer2.Text = Convert.ToString(dataReader["club2"]);
                
                if (!DBNull.Value.Equals(dataReader["date"]))
                {
                    dtpDate.Value = Convert.ToDateTime(dataReader["date"]);
                }

                if (!DBNull.Value.Equals(dataReader[5]))
                {
                    lStadion.Text = Convert.ToString(dataReader[5]);

                    if (!DBNull.Value.Equals(dataReader[6]))
                        lStadion.Text = lStadion.Text + " г." + Convert.ToString(dataReader[6]);
                }
            }

            dataReader.Close();
            conn.Close();

        }

        private void UpdateGoals()
        {

            dgvGoals.Rows.Clear();

            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = Global.connectionString;
            conn.Open();
            SqlCommand myCommand = conn.CreateCommand();
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.CommandText = "[getGoalsOfGame]";
            myCommand.Parameters.Add("@id", SqlDbType.Int).Value = gameId;
            SqlDataReader dataReader = myCommand.ExecuteReader();
            while (dataReader.Read())
            {
                dgvGoals.Rows.Add(dataReader[0].ToString(), dataReader[1].ToString(), dataReader[2].ToString(), dataReader[3].ToString());
            }

            dataReader.Close();
            conn.Close();

            dgvGoals.ClearSelection();

            conn = new SqlConnection();
            conn.ConnectionString = Global.connectionString;
            conn.Open();
            myCommand = conn.CreateCommand();
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.CommandText = "[getGameCount]";
            myCommand.Parameters.Add("@id", SqlDbType.Int).Value = gameId;
            dataReader = myCommand.ExecuteReader();
            if (dataReader.Read())
            {
                Count.Text = dataReader[0] + " - " + dataReader[1];
            }

            dataReader.Close();
            conn.Close();

        }

        private void checkEditState()
        {

            if (dgvGoals.SelectedRows.Count > 0)
                {
                    del.Enabled = true;
                }
                else
                {
                    del.Enabled = false;
                }
            
        }

        private void dgvGoals_SelectionChanged(object sender, EventArgs e)
        {
            checkEditState();
        }

        private void save_Click(object sender, EventArgs e)
        {
            //setGameDate

            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = Global.connectionString;
            conn.Open();
            SqlCommand myCommand = conn.CreateCommand();
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.CommandText = "[setGameDate]";
            myCommand.Parameters.Add("@id", SqlDbType.Int).Value = gameId;
            myCommand.Parameters.Add("@date", SqlDbType.Date).Value = dtpDate.Value;
            myCommand.ExecuteScalar();
            conn.Close();
        }

        private void add1_Click(object sender, EventArgs e)
        {
            GoalInfo gi = new GoalInfo(fk_club1,gameId);
            gi.ShowDialog();

            UpdateGoals();
        }

        private void add2_Click(object sender, EventArgs e)
        {
            GoalInfo gi = new GoalInfo(fk_club2, gameId);
            gi.ShowDialog();

            UpdateGoals();
        }

        private void del_Click(object sender, EventArgs e)
        {
            //delGoal

            int goalId = Convert.ToInt32(dgvGoals.SelectedRows[0].Cells["id"].Value);

            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = Global.connectionString;
            conn.Open();
            SqlCommand myCommand = conn.CreateCommand();
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.CommandText = "[delGoal]";
            myCommand.Parameters.Add("@id", SqlDbType.Int).Value = goalId;
            myCommand.ExecuteScalar();
            conn.Close();

            UpdateGoals();
        }
    }
}
